<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/5/5
 * Time: 11:51
 */

namespace app\shopping\controller;

use app\shopping\model\Commdetailstool;

class Shoppinguseraddress extends Commdetailstool{
    public function __construct()
    {
        parent::__construct();
    }

    public function index(){
        $action = $_GET["action"];
        $uid = $_GET["uid"];

        if ($action == 'get') {
            $this->getAddress($uid);
        } else if ($action == 'join') {
            $this->joinAddress($uid);
        } else if ($action == 'cancel') {
            $this->cancelAddress($uid);
        } else if ($action == 'edit') {
            $this->editAddress($uid);
        } else if ($action == 'setdefaultstate') {
            $this->setDefaultState($uid);
        }
    }

    /**
     *  得到地址
     * @param str $uid 账号
     */
    function getAddress($uid) {
        $sql = "SELECT id,name,address,detailed_address,default_state ,tel FROM sopp_user_address WHERE  uid='$uid' and is_show=1 ORDER BY datetime DESC";
        $result = $this->mysql->selectSQLAll($sql);
        if (is_array($result)) {
            $jsonArray = array();
            $defaultArray = array();
            foreach ($result as $row) {
                if (!empty($row['id']) && !empty($row['address']) && !empty($row['detailed_address'])) {
                    !empty($row['name']) ? $name = $row['name'] : $name = null;
                    !empty($row['default_state']) ? $default_state = true : $default_state = false;
                    !empty($row['tel']) ? $tel = $row['tel'] : $tel = null;
                    if ($default_state) {
                        $defaultArray = array('id' => $row['id'], 'name' => $name, 'address' => $row['address'], 'detailed_address' => $row['detailed_address'],
                            'default_state' => $default_state, 'tel' => $tel);
                    } else {
                        $jsonArray[] = array('id' => $row['id'], 'name' => $name, 'address' => $row['address'], 'detailed_address' => $row['detailed_address'],
                            'default_state' => $default_state, 'tel' => $tel);
                    }
                }
            }
            count($defaultArray) > 0 ? array_unshift($jsonArray, $defaultArray) : '';
            if (count($jsonArray) > 0) {
                echo echoJson(1, $jsonArray);
            } else {
                echo echoJson(-1, '暂无数据!');
            }
        } else {
            echo echoJson(-1, '暂无数据!');
        }
    }

    /**
     *  添加地址
     * @param str $uid 账号
     */
    function joinAddress($uid) {
        $name = getParameter("name");
        $address = getParameter("address");
        $detailed_address = getParameter("detailed_address");
        $tel = getParameter("tel");
        $default_state = getParameter("default_state");
        !empty($default_state) && (boolean) $default_state ? $default_state = 1 : $default_state = 0;
        if (!$this->getJoinAddressParameterIsOk($name, $address, $detailed_address, $tel)) {
            return;
        }
        beginTransactionSQL(); //开始事务
        if ($default_state == 1) {
            $sql = "UPDATE sopp_user_address SET default_state=0 WHERE uid='$uid'";
            if (!$this->mysql->updateSQL($sql)) {
                $this->mysql->rollbackTransactionSQL(); //滚回事务
                echo echoJson(-1, "编辑地址失败,-1！");
                return;
            }
        }
        $sql = "INSERT INTO sopp_user_address (uid,name,address,detailed_address,tel,default_state,datetime) VALUES ('$uid','$name','$address','$detailed_address','$tel','$default_state',now())";
        if ($this->mysql->insertSQL($sql)) {//只要有一个执行失败就滚回
            $this->mysql->submitTransactionSQL(); //提交事务
            echo echoJson(1, "添加成功!");
        } else {
            $this->mysql->rollbackTransactionSQL(); //滚回事务
            echo echoJson(0, "添加失败或地址已存在!");
        }
    }

    /**
     *  判断添加地址时的参数是否正确
     */
    function getJoinAddressParameterIsOk($name, $address, $detailed_address, $tel) {
        if (empty($name)) {
            echo echoJson(0, "收货人为空!");
            return false;
        }
        if (empty($address)) {
            echo echoJson(0, "省市区县为空!");
            return false;
        }
        if (empty($detailed_address)) {
            echo echoJson(0, "详细地址为空!");
            return false;
        }
        if (empty($tel)) {
            echo echoJson(0, "联系电话为空!");
            return false;
        }
        return true;
    }

    /**
     *  删除地址
     * @param str $uid 账号
     */
    function cancelAddress($uid) {
        $id = getParameter("id");
        if (empty($id) && !is_int($id)) {
            echo echoJson(0, "删除地址失败,-1！");
            return;
        }
        $sql = "UPDATE sopp_user_address SET is_show=0,default_state=0 WHERE  uid='$uid' AND id='$id'";
        if ($this->mysql->deleteSQL($sql)) {
            echo echoJson(1, "删除成功！");
        } else {
            echo echoJson(-1, "删除失败！");
        }
    }

    /**
     *  编辑地址
     * @param str $uid 账号
     */
    function editAddress($uid) {
        $id = getParameter("id");
        if (empty($id) && !is_int($id)) {
            echo echoJson(0, "编辑地址失败,-1！");
            return;
        }
        $updatesql = '';
        $name = getParameter("name");
        $address = getParameter("address");
        $detailed_address = getParameter("detailed_address");
        $tel = getParameter("tel");
        $default_state = getParameter("default_state");
        !empty($default_state) ? $default_state = 1 : $default_state = 0;
        if ($this->getJoinAddressParameterIsOk($name, $address, $detailed_address, $tel)) {
            $this->mysql->beginTransactionSQL(); //开始事务
            if ($default_state == 1) {
                $sql = "UPDATE sopp_user_address SET default_state=0 WHERE uid='$uid'";
                if (!$this->mysql->updateSQL($sql)) {
                    $this->mysql->rollbackTransactionSQL(); //滚回事务
                    echo echoJson(-1, "编辑地址失败,-1！");
                    return;
                }
            }
            $sql = "UPDATE sopp_user_address SET name='$name',address='$address',detailed_address='$detailed_address',tel='$tel',default_state=$default_state WHERE  uid='$uid' AND id='$id'";
            if ($this->mysql->updateSQL($sql)) {
                $this->mysql->submitTransactionSQL(); //提交事务
                echo echoJson(1, "编辑地址成功！");
            } else {
                $this->mysql->rollbackTransactionSQL(); //滚回事务
                echo echoJson(-1, "编辑地址失败！");
            }
        }
    }

    /**
     *  编辑地址
     * @param str $uid 账号
     */
    function setDefaultState($uid) {
        $id = getParameter("id");
        $default_state = getParameter("default_state");
        !empty($default_state) ? $default_state = 1 : $default_state = 0;
        if (empty($id) && !is_int($id) && $default_state === null) {
            echo echoJson(0, "编辑地址失败,-1！");
            return;
        }
        $this->mysql->beginTransactionSQL(); //开始事务
        if ($default_state == 1) {
            $sql = "UPDATE sopp_user_address SET default_state=0 WHERE uid='$uid'";
            if (!$this->mysql->updateSQL($sql)) {
                $this->mysql->rollbackTransactionSQL(); //滚回事务
                echo echoJson(-1, "编辑地址失败,-2！");
                return;
            }
        }
        $sql = "UPDATE sopp_user_address SET default_state=$default_state WHERE id='$id'";
        if ($this->mysql->updateSQL($sql)) {
            $this->mysql->submitTransactionSQL(); //提交事务
            echo echoJson(1, "操作成功！");
        } else {
            $this->mysql->rollbackTransactionSQL(); //滚回事务
            echo echoJson(-1, "操作失败！");
        }
    }
}